#include "ESP8266WiFi.h"
#include "ESP8266WebServer.h"
#include <ESP8266WiFiMulti.h>

ESP8266WiFiMulti wifimulti;

ESP8266WebServer webserver(80);

IPAddress loc_IP(192, 168, 0, 150);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);

bool buttonstate;

void setup()
{
    Serial.begin(115200);
    //在这里添加你需要的WiFi账号密码
    wifimulti.addAP("LAGRANGE", "abcdef..");
    wifimulti.addAP("OpenWRT-2.4G", "skabcdef");
    wifimulti.addAP("MEIZU 16s Pro", "skabcdef");
    wifimulti.addAP("Feng", "1234567890");

    Serial.println("\n");

    pinMode(LED_BUILTIN, OUTPUT);
    digitalWrite(LED_BUILTIN, HIGH);

    if (!WiFi.config(loc_IP, gateway, subnet))
        Serial.println("Failed to config WiFi!");

    Serial.print("WiFi Connecting ...");
    while (wifimulti.run() != WL_CONNECTED)
    {
        delay(250);
        Serial.print(".");
    }
    Serial.print("WiFi Connected!");

    Serial.println(WiFi.SSID());
    Serial.println(WiFi.localIP());

    webserver.on("/update", handleUpdate);

    webserver.begin();
    Serial.println("HTTP server started!");
    buttonstate = false;
}

void loop()
{
    webserver.handleClient();
}

void handleUpdate()
{
    
    float floatV = webserver.arg("float").toFloat();
    int intV = webserver.arg("int").toInt();
    int buttonV = webserver.arg("button").toInt();

    webserver.send(200, "text/plain", "Received");

    if (buttonV==0)
    {
        if (buttonstate)
            digitalWrite(LED_BUILTIN, HIGH);
        else
            digitalWrite(LED_BUILTIN, LOW);
        buttonstate = !buttonstate;
    }
}